package com.spzx.channel.service.impl;

import com.alibaba.fastjson.JSON;
import com.spzx.channel.domain.ItemVo;
import com.spzx.channel.service.IItemService;
import com.spzx.common.core.constant.SecurityConstants;
import com.spzx.common.core.domain.R;
import com.spzx.common.core.exception.ServiceException;
import com.spzx.product.api.RemoteProductService;
import com.spzx.product.domain.*;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.Map;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.function.Consumer;
import java.util.function.Function;
import java.util.function.Supplier;

@Service
@Slf4j
public class ItemServiceImpl implements IItemService {

    @Autowired
    private RemoteProductService remoteProductService;

    @Autowired
    ThreadPoolExecutor threadPool;

    @Override
    public ItemVo item(Long skuId) {
        ItemVo itemVo = new ItemVo();

        CompletableFuture<ProductSku> productSkuFuture = CompletableFuture.supplyAsync(new Supplier<ProductSku>() {
            @Override
            public ProductSku get() {
                //获取sku信息
                R<ProductSku> productSkuResult = remoteProductService.getProductSku(skuId, SecurityConstants.INNER);
                if (R.FAIL == productSkuResult.getCode()) {
                    throw new ServiceException(productSkuResult.getMsg());
                }
                ProductSku productSku = productSkuResult.getData();
                itemVo.setProductSku(productSku);
                return productSku;
            }
        },threadPool);


        CompletableFuture<Product> productFuture = productSkuFuture.thenApplyAsync(new Function<ProductSku, Product>() {
            @Override
            public Product apply(ProductSku productSku) {
                //获取商品信息
                R<Product> productResult = remoteProductService.getProduct(productSku.getProductId(), SecurityConstants.INNER);
                if (R.FAIL == productResult.getCode()) {
                    throw new ServiceException(productResult.getMsg());
                }
                Product product = productResult.getData();
                itemVo.setProduct(product);
                itemVo.setSliderUrlList(Arrays.asList(product.getSliderUrls().split(",")));
                itemVo.setSpecValueList(JSON.parseArray(product.getSpecValue()));
                return product;
            }
        },threadPool);


        CompletableFuture skuPriceCompletableFuture = CompletableFuture.runAsync(new Runnable() {
            @Override
            public void run() {
                //获取商品最新价格
                R<SkuPrice> skuPriceResult = remoteProductService.getSkuPrice(skuId, SecurityConstants.INNER);
                if (R.FAIL == skuPriceResult.getCode()) {
                    throw new ServiceException(skuPriceResult.getMsg());
                }
                SkuPrice skuPrice = skuPriceResult.getData();
                itemVo.setSkuPrice(skuPrice);
            }
        },threadPool);



        CompletableFuture detailsCompletableFuture = productSkuFuture.thenAcceptAsync(new Consumer<ProductSku>() {
            @Override
            public void accept(ProductSku productSku) {
                //获取商品详情
                R<ProductDetails> productDetailsResult = remoteProductService.getProductDetails(productSku.getProductId(), SecurityConstants.INNER);
                if (R.FAIL == productDetailsResult.getCode()) {
                    throw new ServiceException(productDetailsResult.getMsg());
                }
                ProductDetails productDetails = productDetailsResult.getData();
                itemVo.setDetailsimagesUrlList(Arrays.asList(productDetails.getImageUrls().split(",")));
            }
        },threadPool);

        CompletableFuture skuSpecMapCompletableFuture = productSkuFuture.thenAcceptAsync(new Consumer<ProductSku>() {
            @Override
            public void accept(ProductSku productSku) {
                R<Map<String, Long>> skuSpecValueResult = remoteProductService.getSkuSpecValue(productSku.getProductId(), SecurityConstants.INNER);
                if (R.FAIL == skuSpecValueResult.getCode()) {
                    throw new ServiceException(skuSpecValueResult.getMsg());
                }
                Map<String, Long> skuSpecValueMap = skuSpecValueResult.getData();
                itemVo.setSkuSpecValueMap(skuSpecValueMap);
            }
        },threadPool);
        //获取商品规格对应商品skuId信息

        CompletableFuture skuSrockCompletable = CompletableFuture.runAsync(new Runnable() {
            @Override
            public void run() {
                //获取商品库存信息
                R<SkuStockVo> skuStockResult = remoteProductService.getSkuStock(skuId, SecurityConstants.INNER);
                if (R.FAIL == skuStockResult.getCode()) {
                    throw new ServiceException(skuStockResult.getMsg());
                }
                SkuStockVo skuStockVo = skuStockResult.getData();
                itemVo.setSkuStockVo(skuStockVo);
            }
        },threadPool);

        CompletableFuture.allOf(productSkuFuture,productFuture,skuPriceCompletableFuture,detailsCompletableFuture,skuSpecMapCompletableFuture,skuSrockCompletable).join();
        return itemVo;
    }

//    ItemVo itemVo = new ItemVo();
//
//    //获取sku信息
//    R<ProductSku> productSkuResult = remoteProductService.getProductSku(skuId, SecurityConstants.INNER);
//        if (R.FAIL == productSkuResult.getCode()) {
//        throw new ServiceException(productSkuResult.getMsg());
//    }
//    ProductSku productSku = productSkuResult.getData();
//        itemVo.setProductSku(productSku);
//
//    //获取商品信息
//    R<Product> productResult = remoteProductService.getProduct(productSku.getProductId(), SecurityConstants.INNER);
//        if (R.FAIL == productResult.getCode()) {
//        throw new ServiceException(productResult.getMsg());
//    }
//    Product product = productResult.getData();
//        itemVo.setProduct(product);
//        itemVo.setSliderUrlList(Arrays.asList(product.getSliderUrls().split(",")));
//        itemVo.setSpecValueList(JSON.parseArray(product.getSpecValue()));
//
//    //获取商品最新价格
//    R<SkuPrice> skuPriceResult = remoteProductService.getSkuPrice(skuId, SecurityConstants.INNER);
//        if (R.FAIL == skuPriceResult.getCode()) {
//        throw new ServiceException(skuPriceResult.getMsg());
//    }
//    SkuPrice skuPrice = skuPriceResult.getData();
//        itemVo.setSkuPrice(skuPrice);
//
//    //获取商品详情
//    R<ProductDetails> productDetailsResult = remoteProductService.getProductDetails(productSku.getProductId(), SecurityConstants.INNER);
//        if (R.FAIL == productDetailsResult.getCode()) {
//        throw new ServiceException(productDetailsResult.getMsg());
//    }
//    ProductDetails productDetails = productDetailsResult.getData();
//        itemVo.setDetailsimagesUrlList(Arrays.asList(productDetails.getImageUrls().split(",")));
//
//    //获取商品规格对应商品skuId信息
//    R<Map<String, Long>> skuSpecValueResult = remoteProductService.getSkuSpecValue(productSku.getProductId(), SecurityConstants.INNER);
//        if (R.FAIL == skuSpecValueResult.getCode()) {
//        throw new ServiceException(skuSpecValueResult.getMsg());
//    }
//    Map<String, Long> skuSpecValueMap = skuSpecValueResult.getData();
//        itemVo.setSkuSpecValueMap(skuSpecValueMap);
//
//    //获取商品库存信息
//    R<SkuStockVo> skuStockResult = remoteProductService.getSkuStock(skuId, SecurityConstants.INNER);
//        if (R.FAIL == skuStockResult.getCode()) {
//        throw new ServiceException(skuStockResult.getMsg());
//    }
//    SkuStockVo skuStockVo = skuStockResult.getData();
//        itemVo.setSkuStockVo(skuStockVo);
//
//        return itemVo;
//}
}